using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class UImNavViewor : UImContainer
{
	
		public UINav naviationBar;
		protected List<UIViewor> listViewors;
	
		override protected void initComponent ()
		{
				base.initComponent ();
				listViewors = GameObjectUtils.getChildrenGameObjectComponentSort<UIViewor> (containerTrans.gameObject);
		
				int count = listViewors.Count;
				if (count > 0) {
						foreach (UIViewor viewor in listViewors) {
								GameObjectUtils.getGameObjectComponent<UIRetain> (viewor.gameObject).retain ();
								VieworUtils.removeView (viewor.gameObject.transform);
						}
			
						VieworUtils.doAppear (listViewors [count - 1], containerTrans);
						display ();
				}
		}
	
		public void display ()
		{
				if (naviationBar != null) {
						naviationBar.display (listViewors);
				}
		}
	
		public void pushViewor (UIViewor viewor)
		{
				int count = listViewors.Count;
				if (count > 0) {
						VieworUtils.doDisappear (listViewors [count - 1]);
				}
		
				listViewors.Add (viewor);
				VieworUtils.doAppear (viewor, containerTrans);
				display ();
		}
	
		public void popViewor ()
		{
				int count = listViewors.Count;
				if (count > 1) {
						count = count - 1;
						UIViewor viewor = listViewors [count];
						Destroy (viewor.gameObject);
						listViewors.RemoveAt (count);
			
						VieworUtils.doAppear (listViewors [count - 1], containerTrans);
						display ();
				}
		}
	
		public void pushGameObject (GameObject go)
		{
				pushViewor (GameObjectUtils.getGameObjectComponent<UIViewor> (go));
		}
	
		public void popVieworRoot ()
		{		
				int count = listViewors.Count;
				if (count > 1) {
						for (--count; count > 0; count--) {
								UIViewor viewor = listViewors [count];
								Destroy (viewor.gameObject);
						}
			
						listViewors.RemoveRange (1, listViewors.Count - 1);
			
						VieworUtils.doAppear (listViewors [0], containerTrans);
						display ();
				}
		}

		public override bool doAppear ()
		{
				if (base.doAppear ()) {
						if (listViewors != null) {
								listViewors [listViewors.Count - 1].doAppear ();
						}

						return true;
				}

				return false;
		}
	
		public override bool doDisappear ()
		{
				if (base.doDisappear ()) {
						if (listViewors != null) {
								listViewors [listViewors.Count - 1].doDisappear ();
						}
			
						return true;
				}
		
				return false;
		}
}
